﻿using Iworld.Client.Framework.AuthorService;
using Iworld.ViewModel;
using System;
using System.Net;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Iworld.Client.Framework
{
    /// <summary>
    /// 登陆页的视图模型
    /// </summary>
    public class LoginViewModel : ViewModelBase
    {
        #region 私有字段

        string _account = "test";
        string _password = "test";
        string _loadingMessage = "";
        bool rememberMe;
        #endregion

        #region 公开属性

        public bool RememberMe
        {
            get
            {
                return rememberMe;
            }
            set
            {
                if (rememberMe != value)
                {
                    rememberMe = value;
                    OnPropertyChanged("RememberMe");
                }
            }
        }

        /// <summary>
        /// 账号
        /// </summary>
        public string Account
        {
            get
            {
                return _account;
            }
            set
            {
                if (_account != value)
                {
                    _account = value;
                    OnPropertyChanged("Account");
                }
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                if (_password != value)
                {
                    _password = value;
                    OnPropertyChanged("Password");
                }
            }
        }

        /// <summary>
        /// 加载时的文字显示
        /// </summary>
        public string LoadingMessage
        {
            get
            {
                return _loadingMessage;
            }
            set
            {
                if (_loadingMessage != value)
                {
                    _loadingMessage = value;
                    OnPropertyChanged("LoadingMessage");
                }
            }
        }

        /// <summary>
        /// 登陆通知
        /// </summary>
        public UniversalCommand LoginCommand { get; set; }

        /// <summary>
        /// 跳转命令
        /// </summary>
        public UniversalCommand JumpCommand { get; set; }

        #endregion

        /// <summary>
        /// 实例化一个新的登陆页的视图模型
        /// </summary>
        public LoginViewModel()
        {
            this.LoginCommand = new UniversalCommand(Login);
            this.JumpCommand = new UniversalCommand(x=> ClientViewModelService.Current.JumpTo(x.ToString()));
            bool hadRememberMe = DataManager.HaveValue<LoginPackage>(DataKey.RememberMe,true);
            if (hadRememberMe)
            {
                LoginPackage package = DataManager.GetValue<LoginPackage>(DataKey.RememberMe,true);
                this.Account = package.Account;
                this.Password = package.Password;
                this.RememberMe = true;
            }
        }

        void Login(object parameter)
        {
           if (IsBusy)
            {
                return;
            }
            this.LoadingMessage = "正在登录请稍后......";
            IsBusy = true;
            if (this.RememberMe == true)
            {
                LoginPackage package = new LoginPackage(this.Account, this.Password);
                DataManager.SetValue(DataKey.RememberMe, package,true);
            }
            else
            {
                DataManager.RemoveValue(DataKey.RememberMe,true);
            }
           AuthorServiceClient asc = new AuthorServiceClient();
            asc.LoginCompleted += LoginCompleted;
            asc.LoginAsync(new LoginImport() { 
              Account = this.Account,
              Password = this.Password,
            });  
        }

        void LoginCompleted(object sender, LoginCompletedEventArgs e)
        {
            if(e.Result.Success)
            {
                string tk = e.Result.Info;
                DataManager.SetValue(DataKey.Iworld_Token,tk);
                AuthorServiceClient asc = new AuthorServiceClient();
                asc.GetUserSelfInfoCompleted+=asc_GetUserSelfInfoCompleted;
                asc.GetUserSelfInfoAsync(new GetUserSelfInfoImport() { Token=tk });
                this.LoadingMessage = "正在加载用户信息......";
            }
            else
            {
               string error = e.Result.Error;
               this.ShowError(error);
               IsBusy = false;
            }
         
        }

        void asc_GetUserSelfInfoCompleted(object sender, GetUserSelfInfoCompletedEventArgs e)
        {
            IsBusy = false;
            if (e.Result.Success)
            {
                DataManager.SetValue(DataKey.User_SelfInfo, e.Result.Info);
                ClientViewModelService.Current.JumpTo(Page.大厅主页);
            }
            else
            {
                string error = e.Result.Error;
                this.ShowError(error);
            }
          
        }
    }

    /// <summary>
    /// 用户登陆信息的封装
    /// </summary>
    [DataContract]
    public class LoginPackage
    {
        #region 属性

        /// <summary>
        /// 用户名
        /// </summary>
        [DataMember]
        public string Account { get; set; }

        /// <summary>
        /// 密码
        /// </summary>
        [DataMember]
        public string Password { get; set; }

        #endregion

        #region 构造方法

        /// <summary>
        /// 实例化一个用户登陆信息的封装
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="password">密码</param>
        public LoginPackage(string acc, string password)
        {
            this.Account = acc;
            this.Password = password;
        }

        #endregion
    }
}
